package com.ai.service.made.impl;

import com.ai.common.constant.FunctionTypeConstants;
import com.ai.entity.made.CoalCraneInspection;
import com.ai.mapper.made.CoalCraneInspectionMapper;
import com.ai.service.made.CameraFunctionsService;
import com.ai.service.made.FunctionService;
import com.ai.service.made.ICoalCraneInspectionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiaok
 * @since 2024-11-04
 */
@Service
public class CoalCraneInspectionServiceImpl extends ServiceImpl<CoalCraneInspectionMapper, CoalCraneInspection> implements ICoalCraneInspectionService {

    @Resource
    private CameraFunctionsService cameraFunctionsService;

    @Override
    public List<CoalCraneInspection> getCraneInspections(Integer cameraFunctionId) {
        return this.list(new QueryWrapper<CoalCraneInspection>().select("begin_time", "end_time", "single_maintenance_duration", "accumulated_maintenance_time")
                .eq("camera_function_id", cameraFunctionId)
                .eq("function_type", FunctionTypeConstants.MONORAIL_STATION_DETECTION));
    }

    @Override
    public String getMaintenanceState(Integer accumulatedDuration, String cameraId, String functionType) {

        String relationId = cameraFunctionsService.getRelationByCameraIdFunction(cameraId,functionType);
        if (null != relationId){
            List<CoalCraneInspection> coalCraneInspectionList = this.list(new QueryWrapper<CoalCraneInspection>().eq("camera_function_id", relationId));
            if (!CollectionUtils.isEmpty(coalCraneInspectionList)){
                CoalCraneInspection coalCraneInspection = coalCraneInspectionList.get(0);
                String accumulatedMaintenanceTime = coalCraneInspection.getAccumulatedMaintenanceTime();
                return accumulatedDuration>=Integer.parseInt(accumulatedMaintenanceTime)?"2":"3";
            }
        }


        return "1";
    }
}
